package com.sx.manage.service.file;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.sx.manage.biz.file.ICertificateAddressBiz;
import com.sx.manage.service.sys.IMBaseSystemConfigInfoService;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.model.constants.MSystemConstants;
import com.sx.model.constants.type.consumer.MConsumersNatures;
import com.sx.model.manage.mybatis.form.file.MCertificateAddressForm;
import com.sx.model.manage.mybatis.form.sys.MSystemConfigInfoForm;
import com.sx.model.manage.mybatis.vo.consumer.login.MConsumerLoginVO;
import com.sx.model.manage.mybatis.vo.file.MCertificateAddressVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultUploadVO;
import com.sx.model.manage.mybatis.vo.sys.MSysConfigVO;

/**
 * 证件地址信息
 * @author Administrator
 *
 */
@Service("companyCertificateWebServiceImpl")
public class MCompanyCertificateWebServiceImpl {
	/**
	 * 日志
	 */
	private Logger log = LoggerFactory.getLogger(this.getClass());
	//证件地址信息
	@Resource(name="certificateAddressBizImpl")
	private ICertificateAddressBiz certificateAddressBizImpl;
	//系统参数配置
	@Resource(name = "mBaseSystemConfigInfoServiceImpl")
	private IMBaseSystemConfigInfoService mBaseSystemConfigInfoServiceImpl;
	
	/** 跳转到证件上传界面**/
	public ViewAndModel certificate_index(ViewAndModel viewAndModel,Long company_id) {
		log.info("跳转到证件上传界面");
		// 如需在添加页需要加载元素在此处增加
		viewAndModel.setViewName("certificate/company/certificate_index");
		viewAndModel.addObject("company_id", company_id);
		return viewAndModel;
	}
	/** 跳转到证件上传界面**/
	public ViewAndModel certificate_toAdd(ViewAndModel viewAndModel,Long company_id) {
		log.info("跳转到证件上传界面");
		// 如需在添加页需要加载元素在此处增加
		viewAndModel.setViewName("certificate/company/addCertificate");
		viewAndModel.addObject("company_id", company_id);
		return viewAndModel;
	}
	/** 跳转到证件编辑界面**/
	public ViewAndModel certificate_toEdit(ViewAndModel viewAndModel,Long id) {
		log.info("跳转到证件上传界面");
		// 如需在添加页需要加载元素在此处增加
		viewAndModel.setViewName("certificate/company/editCertificate");
		MCertificateAddressVO addressVO = certificateAddressBizImpl.getCertificateAddressById(id).getObjVo();
		viewAndModel.addObject("addressVO", addressVO);
		return viewAndModel;
	}
	/** 保存图片信息**/
	public ResultBooleanVO certificate_save(MCertificateAddressForm form,MConsumerLoginVO loginConsumer) {
		log.info("保存证件图片信息form={}",form);
		form.setFile_nature(MConsumersNatures.COMPANY);
		return certificateAddressBizImpl.saveCompanyCertificate(form,loginConsumer);
	}
	/** 更新图片信息**/
	public ResultBooleanVO certificate_update(MCertificateAddressForm form,MConsumerLoginVO loginConsumer) {
		log.info("保存证件图片信息form={}",form);
		return certificateAddressBizImpl.updateCertificate(form,loginConsumer);
	}
	/** 查询数据列表**/
	public ResultListVO<MCertificateAddressVO> certificate_page(MCertificateAddressForm form) {
		log.info("查询数据翻页信息form={}",form);
		ResultListVO<MCertificateAddressVO> result = certificateAddressBizImpl.queryPage(form);
		
		List<MCertificateAddressVO> list = result.getData();
		if (!list.isEmpty()) {
			set_file_url(list);
		}
		return result;
	}
	
	/** 上传图片到服务器**/
	public ResultUploadVO<String> upload(MultipartFile file, HttpServletRequest request) {
		String paths = null;
		String path = request.getSession().getServletContext().getRealPath("upload");
		String fileName = file.getOriginalFilename();
		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		try {
			file.transferTo(targetFile);
			//paths = OSSUtils.uploadFile(targetFile, MSystemConstants.CERTIFICATE_UPLOAD_URL_);
			boolean flag = targetFile.delete();
			if (null == paths) {
				return new ResultUploadVO<>("sj.m.common.upload.error");
			}
			log.info("上传文件成功，路径paths={},flag={} :" ,paths,flag);
		}catch (Exception e) {
			log.error("文件上传异常error={}",e);
		}
		ResultUploadVO<String> result = new ResultUploadVO<>();
		result.setSuccess(true);
		result.setMsgcode("sj.m.common.upload.success");
		result.setPath(paths);
		return result;
	}
	
	/**增加下载路径处理*/
	public void set_file_url(List<MCertificateAddressVO> list){
		String template_url= template_url();
		for(MCertificateAddressVO vo:list){
			if (null != vo) {
				vo.setFile_address(template_url+vo.getFile_address());
			}
		}
	}
	public String template_url() {
		MSystemConfigInfoForm form = new MSystemConfigInfoForm();
		form.setConfig_name(MSystemConstants.TEMPLATE_DOWNLOAD_URL);
		MSysConfigVO configVO = mBaseSystemConfigInfoServiceImpl.queryByIdSystemConfig(form).getObjVo();
		if(null==configVO) {
			return null;
		}
		return configVO.getConfig_value();
	}

}
